package com.sf.service.impl;

import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.sf.framework.domain.Result;
import com.sf.manager.RegInfoManager;
import com.sf.model.RegInfo;
import com.sf.model.RegInfoExample;
import com.sf.model.RegInfoExample.Criteria;
import com.sf.service.RegInfoRestService;

/**
 * 报名信息restful服务实现类
 *
 * @author 604625
*/
@Service
public class RegInfoRestServiceImpl implements RegInfoRestService {

	private Logger logger = LoggerFactory.getLogger(getClass());
			
	@Autowired
	RegInfoManager regInfoManager;
	
	


	/**
	 * 新增
	 * 
	 * @param record 记录
	 * 
	 * @return Result<Integer>
	 *
    */
	public Result<Integer> insert(RegInfo record){
	    Result<Integer> result = new Result<>(true); 
		if(record == null){
			throw new IllegalStateException("record不能为空");
		}
    	
		int count = 0;
		try{
			count = regInfoManager.insert(record);
			result.setObj(Integer.valueOf(count));
		}
		catch(Exception e){
			result.setSuccess(false);
			result.setObj(Integer.valueOf(count));
			result.setErrorMessage("insert error");
			logger.error("insert error", e);
		}
		return result;
	}
	
	

	/**
	 * 按主键查询
	 * 
	 * @param id 
	 * 
	 * @return  List 
	 *
    */
    public Result<RegInfo> selectByPrimaryKey(Integer id){
	    Result<RegInfo> result = new Result<>(true);
    	if(id == null){
    		throw new IllegalStateException("参数id不能为空");
    	}
		RegInfo record = null;
		try{
			record = regInfoManager.selectByPrimaryKey(id);
			result.setObj(record);
		}
		catch(Exception e){
			result.setSuccess(false);
			result.setErrorMessage("selectByPrimaryKey error");
			logger.error("selectByPrimaryKey error", e);
		}
		return result;
	}

	/**
	 * 按主键更新,null的字段不更新
	 * 
	 * @param record 记录
	 * @return  List 
	 *
    */
    public Result<Integer> updateByPrimaryKeySelective(RegInfo record){
	    Result<Integer> result = new Result<>(true);
		if(record == null){
			throw new IllegalStateException("参数record不能为空");
		}
    	if(record.getId() == null){
    		throw new IllegalStateException("主键id属性不能为空");
    	}
		int count = 0;
		try{
			count = regInfoManager.updateByPrimaryKeySelective(record);
			result.setObj(Integer.valueOf(count));
		}
		catch(Exception e){
			result.setSuccess(false);
			result.setObj(Integer.valueOf(count));
			result.setErrorMessage("updateByPrimaryKeySelective error");
			logger.error("updateByPrimaryKeySelective error", e);
		}
		return result;
	}

	/**
	 * 按主键更新
	 * 
	 * @param record 记录
	 * @return Result<Integer>
	 *
    */
	public Result<Integer> updateByPrimaryKey(RegInfo record){
	    Result<Integer> result = new Result<>(true);
		if(record == null){
			throw new IllegalStateException("参数record不能为空");
		}
    	if(record.getId() == null){
    		throw new IllegalStateException("主键id属性不能为空");
    	}
		int count = 0;
		try{
			count = regInfoManager.updateByPrimaryKey(record);
			result.setObj(Integer.valueOf(count));
		}
		catch(Exception e){
			result.setSuccess(false);
			result.setObj(Integer.valueOf(count));
			result.setErrorMessage("updateByPrimaryKey error");
			logger.error("updateByPrimaryKey error", e);
		}
		return result;
	}
	
	
	public Result<RegInfo> selectByOpenId(String openId) {
		Result<RegInfo> result = new Result<>(true);
		if (StringUtils.isBlank(openId)) {
			throw new IllegalStateException("参数openId不能为空");
		}
		RegInfo record = null;
		try {
			// 按openId查询寄件人信息
			RegInfoExample query = new RegInfoExample();
			Criteria criteria = query.createCriteria();
			criteria.andOpenidEqualTo(openId);
			List<RegInfo> regInfos = regInfoManager.selectByExample(query);
			
			if(CollectionUtils.isNotEmpty(regInfos)){
				record = regInfos.get(0);
			}
			result.setObj(record);
		} catch (Exception e) {
			result.setSuccess(false);
			result.setErrorMessage("selectByOpenId error");
			logger.error("selectByOpenId error", e);
		}
		return result;
	}
}